<?php

namespace app\controller;

use app\BaseController;
use app\model\Character;
use app\model\Character_jurisdiction;

/**
 * 部门列表
 * Class CharacterController
 * @package app\controller
 * @author 下雨吧จุ๊บ
 */
class CharacterController extends BaseController
{
    /**
     * @apiGroup 部门
     * @apiVersion 1.0.0
     * @api {GET} /character  部门列表
     * @apiDescription 部门列表
     * @apiParam {String} listRows 每页数量(必填*)
     * @apiParam {String} page 当前页数(必填*)
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * data: 部门列表,
     * message: '获取部门列表成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function index()
    {
        $data = input();
        if (empty($data['listRows'])) {
            $data['listRows'] = '2';
        }
        if (empty($data['page'])) {
            $data['page'] = '1';
        }
        $map[] = ['title','like','%%'];
        if (!empty($data['map']['title'])){
            $map[] = ['title','like','%'. $data['map']['title'] .'%'];
        }
        if (!empty($data['map']['status'])){
            $map[] = ['status','=',$data['map']['status']];
        }
        if (!empty($data['map']['author'])){
            $map[] = ['author','=',$data['map']['author']];
        }
        $data['list'] = Character::where($map)
            ->paginate([
            'list_rows' => $data['listRows'],
            'page' => $data['page'],
        ]);
        //员工列表
        $data['user'] = \app\model\User::where('status',1)->select();
        success($data, '获取部门列表成功');
    }

    /**
     * @apiGroup 部门
     * @apiVersion 1.0.0
     * @api {POST} /character  新增保存
     * @apiDescription 新增保存
     * @apiParam {String} title 职位名称(必填*)
     * @apiParam {String} status 状态(必填*)
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '新增成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function save()
    {
        $data = input();
        //必填项验证
        if (empty($data['title'])) {
            error('请填写职位名称');
        }
        //重复验证
        $is_setCharacter = Character::where('title', $data['title'])->field('id')->find();
        if (!empty($is_setCharacter)) {
            error('该职位名称已存在');
        }
        //补全信息
        $data['author'] = cache($_SESSION['token']['value'])['realname'];
        $data['updateauthor'] = $data['author'];
        $data['createtime'] = time();
        $data['updatetime'] = time();
        //添加
        $result = Character::insert($data);
        if ($result) {
            success(array(), '新增成功');
        }
        error('新增失败');
    }

    /**
     * @apiGroup 部门
     * @apiVersion 1.0.0
     * @api {GET} /character/:id  编辑读取
     * @apiDescription 编辑页面(:id是指部门id,直接写在路由中)
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * data: array
     * message: '获取数据成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function read()
    {
        $data = input();
        //必填项验证
        if (empty($data['id'])) {
            error('520数据错误(Missing parameter id)');
        }
        //返回部门详情
        $data['info'] = Character::where('id', $data['id'])->find();
        success($data, '获取数据成功');
    }

    /**
     * @apiGroup 部门
     * @apiVersion 1.0.0
     * @api {PUT} /character/:id  编辑保存
     * @apiDescription 编辑页面保存(:id是指部门id,直接写在路由中)
     * @apiParam {String} title 职位名称(必填*)
     * @apiParam {String} status 状态(必填*)
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '保存成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function update()
    {
        $data = input();
        //必填项验证
        if (empty($data['id'])) {
            error('520数据错误(Missing parameter id)');
        }
        $data['updateauthor'] = cache($_SESSION['token']['value'])['realname'];
        $data['updatetime'] = time();
        //更新数据
        $result = Character::where('id', $data['id'])->save($data);
        success(array(), '保存成功');
    }

    /**
     * @apiGroup 部门
     * @apiVersion 1.0.0
     * @api {DELETE} /character/0  删除
     * @apiDescription 删除(路由中直接写0,ids集合使用参数传递)
     * @apiParam {array} ids id集合
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '删除成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function delete()
    {
        $data = input();
        //必填项验证
        if (empty($data['ids'])) {
            error('520数据错误(Missing parameter id)');
        }
        $result = Character::whereIn('id', $data['ids'])->delete();
        success(array(), '删除成功');
    }

    /**
     * @apiGroup 部门
     * @apiVersion 1.0.0
     * @api {GET} /character/create  职位权限菜单
     * @apiParam {string} characterid 部门id(必填*)
     * @apiDescription 职位权限菜单界面
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * data: 数据,
     * message: '获取数据成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function create()
    {
        $data = input();
        //必填项验证
        if (empty($data['characterid'])) {
            error('520数据错误(Missing parameter characterid)');
        }
        //所有权限列表
        $data['jurisdiction'] = jurisdiction();
        //获取当前部门下的所有权限
        $data['list'] = Character_jurisdiction::alias('cj')
            ->join(['jurisdiction' => 'j'], 'cj.jurisdictionid=j.id')
            ->where('cj.characterid', $data['characterid'])
            ->field('j.id,j.pid,j.title')
            ->select();
        success($data, '获取数据成功');
    }

    /**
     * @apiGroup 部门
     * @apiVersion 1.0.0
     * @api {GET} /character/0/edit  职位权限菜单保存
     * @apiDescription 职位权限菜单界面保存
     * @apiParam {array} ids 权限id集合
     * @apiParam {string} characterid 部门id(必填*)
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '保存成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function edit()
    {
        $data = input();
        //必填项验证
        if (empty($data['characterid'])) {
            error('520数据错误(Missing parameter characterid)');
        }
        //删除现有权限
        $result = Character_jurisdiction::where('characterid', $data['characterid'])->delete();
        if (!empty($data['ids'])) {
            //重新添加新权限
            foreach ($data['ids'] as $k => $v) {
                $insert[$k]['characterid'] = $data['characterid'];
                $insert[$k]['jurisdictionid'] = $v;
            }
            $result = Character_jurisdiction::insertAll($insert);
        }
        success(array(), '保存成功');
    }
}
